package com.ecust.Dao.User.Impl;


import com.ecust.Dao.User.UserDao;
import com.ecust.Entity.User.User;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2017/4/29.
 * @Description: UserDao的实现
 *@author mengweiqiang
 */

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * UserDao FindUser的实现
     *
     * @author mengweiqiang
     */
    public boolean FindUser(String UserID, String UserPwd, String RoleID) {
        int LoginUserNumber = 0;
        String sql = "select * from User left join UserRole on User.UserID = UserRole.UserID where User.UserID ='" + UserID + "'" + "and User.UserPwd ='" + UserPwd + "'" + "and UserRole.RoleID ='" + RoleID + "'";
        try {
            System.out.println("UserDao");
            LoginUserNumber = jdbcTemplate.queryForList(sql).size();
            if (LoginUserNumber == 0) {
                System.out.println("UserDaoFalse");
                return false;
            } else {
                System.out.println("UserDaoTrue");
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("UserDaoException");
            return false;
        }
    }

    /**
     * UserDao FindUserInfo的实现
     *
     * @author mengweiqiang
     */
    public List<User> FindUserInfo(String UserID) {

        try {
            String sql = "select * from User where UserID = '" + UserID + "'";
            List<User> user = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
            return user;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * UserDao CheckUserPassword的实现
     *
     * @author mengweiqiang
     */
    public boolean CheckUserPassword(String UserID, String UserPwd) {
        int ReturnNumber = 0;
        String sql = "select UserPwd from User where UserID=" + UserID + "and UserPwd=" + UserPwd;
        try {
            ReturnNumber = jdbcTemplate.queryForList(sql).size();
            if (ReturnNumber == 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * UserDao ChangeUserPassword的实现
     *
     * @author mengweiqiang
     */
    public boolean ChangeUserPassword(String UserID, String NewPassword) {
        try {
            int RecordNumber = 0;
            String sql = "update User set UserPwd = ? where UserID = ?";
            RecordNumber = jdbcTemplate.update(sql, new Object[]{NewPassword, UserID});
            if (RecordNumber == 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * UserDao ChangeUserInfo的实现
     *
     * @author mengweiqiang
     */
    public boolean ChangeUserInfo(String UserID, String UserName, String Name, int Gender, Date DateOfBirth, String TelPhone, String Email, String Company)
        {
        int RecordNumber = 0;
        try
        {
            String sql = "update User set UserName =?,Name = ?,Gender=?,DateOfBirth = ?,TelPhone=?,Email=?,Company=? where UserID=?";
            RecordNumber = jdbcTemplate.update(sql,new Object[]{UserName,Name.trim(),Gender,DateOfBirth,TelPhone,Email,Company,UserID});
            System.out.println(Name);
            if (RecordNumber == 1) {
                return true;
            } else {
                return false;
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
}
